package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.ExportMapper;
import com.ld.hpfs.service.ExportService;

@Service
public class ExportServiceImpl implements ExportService {

//	private static final Logger logger = LoggerFactory.getLogger(DealServiceImpl.class);
	
	@Autowired
	private ExportMapper exportMapper;
	
	private static final Logger logger = LoggerFactory.getLogger(DealServiceImpl.class);
	
	@Override
	public ArrayList<HashMap<String,Object>> getWriteBackList(HashMap<String,Object> param){
		int count = exportMapper.getWriteBackListCount(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			  return exportMapper.getWriteBackList(param);
		}
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getWriteBackForExport(HashMap<String,Object> param){
		ArrayList<HashMap<String,Object>> head = exportMapper.getWriteBackHeadInfoForExport(param);	
		if(!head.isEmpty()){
		  for(int i=0;i<head.size();i++){
			 String headId = head.get(i).get("ID_HEAD_ID").toString();
			 
//			  String headId = head.get(i).get("DOCUMENT_NO").toString();
			 HashMap<String,Object> middle = new HashMap<String,Object>();
			 middle.put("headId", headId);
			 middle.put("documentno", head.get(i).get("DOCUMENT_NO").toString());
			 middle.put("companyId", head.get(i).get("COMPANY_ID").toString());
			 ArrayList<HashMap<String,Object>> details = exportMapper.getWriteBackDetailInfoForExport(middle);
			 head.get(i).put("details", details);
			 head.get(i).put("count",details.size());
		  }
		  exportMapper.updateWriteBackHeadInfoForExport(param);
		}
		return head;
	}
	
	@Override
	public List<HashMap<String,Object>> getExportExcelList(HashMap<String,Object> param) {
		exportMapper.updateWriteBackHeadInfoForExportExcel(param);
		List<HashMap<String,Object>> list = exportMapper.getExportExcelList(param);
		if(!list.isEmpty()){
			exportMapper.updateWriteBackHeadInfoForExport(param);
		}
		return list;
	}
	
	@Override
	public List<HashMap<String,Object>> getExportExcelListOne(HashMap<String,Object> param) {
		List<HashMap<String,Object>> list = exportMapper.getExportExcelListOne(param);
		return list;
	}
	
	@Override
	public int getCorrectId(HashMap<String, Object> param){
		return exportMapper.getCorrectId(param);
	}
	
	@Override
	public void updateWriteBackHeadInfoForExport(HashMap<String, Object> param){
		exportMapper.updateWriteBackHeadInfoForExport(param);
	}

	@Override
	public HashMap<String, Object> getwritebackplaninfo(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return exportMapper.getwritebackplaninfo(param);
	}

	@Override
	public void updateplaninfo(HashMap<String, Object> param) {
		exportMapper.updateplaninfo(param);
	}

	@Override
	public void saveplaninfo(HashMap<String, Object> param) {
		exportMapper.saveplaninfo(param);
	}

	@Override
	public List<HashMap<String, Object>> getExportWriteBackTask(HashMap<String, Object> param) {
		int count = exportMapper.getExportWriteBackTaskCount(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			  return exportMapper.getExportWriteBackTask(param);
		}
	}

	@Override
	public List<HashMap<String, Object>> getWriteBackListAll(HashMap<String, Object> param) {
		exportMapper.setDocStatusToWB(param);
		return exportMapper.getWriteBackListAll(param);
	}

	@Override
	public HashMap<String, Object> getExportPlan() {
		// TODO Auto-generated method stub
		return exportMapper.getExportPlan();
	}

	@Override
	public void setRunFlag(HashMap<String, Object> param) {
		exportMapper.setRunFlag(param);
	}

	@Override
	public void saveplanTaskinfo(HashMap<String, Object> param) {
		exportMapper.saveplanTaskinfo(param);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getWriteBackListAllTXT(HashMap<String, Object> param) {
		ArrayList<HashMap<String,Object>> head = exportMapper.getWriteBackHeadInfoForExportTXT(param);	
		if(!head.isEmpty()){
		  for(int i=0;i<head.size();i++){
			 String headId = head.get(i).get("ID_HEAD_ID").toString();
//			  String headId = head.get(i).get("DOCUMENT_NO").toString();
			 HashMap<String,Object> middle = new HashMap<String,Object>();
			 middle.put("headId", headId);
			 middle.put("documentno", head.get(i).get("DOCUMENT_NO").toString());
			 middle.put("companyId", head.get(i).get("COMPANY_ID").toString());
			 ArrayList<HashMap<String,Object>> details = exportMapper.getWriteBackDetailInfoForExportTXT(middle);
			 head.get(i).put("details", details);
			 head.get(i).put("count",details.size());
		  }
		  exportMapper.setDocStatusToWB(param);
		}
		return head;
	}

	@Override
	public List<HashMap<String,Object>> getWriteBackWay(HashMap<String, Object> param) {
		ArrayList<HashMap<String,Object>> way = exportMapper.getWriteBackWay(param);
		if(way.isEmpty()){
			return null;
		}else{
//			HashMap<String,Object> WriteBackInfo = way.get(0);
//			String WriteBackWay = (String)WriteBackInfo.get("ACK_ERP_TYPE");
//			if(WriteBackWay!=null){
//				return way;
//			}else{
//				return null;
//			}
			return way;
		}
	}
	
	@Override
	public void ReleaseDealData(HashMap<String, Object> param){
		exportMapper.ReleaseWriteBackHeadInfoForExport(param);
	}

	@Override
	public List<HashMap<String, Object>> getExportColumnNames(
			HashMap<String, Object> viewName) {
		return exportMapper.getExportColumnNames(viewName);
	}
	



@Override
public ArrayList<HashMap<String, Object>> getOrigiDocNo(HashMap<String, Object> Doc){
	return exportMapper.getOrigiDocNo(Doc);
}
}